//
//  TLWalletViewController.m
//  WXReader
//
//  Created by huaya on 2025/9/5.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLWalletViewController.h"

#import "TLWalletHeaderView.h"
#import "TLWalletTableViewCell.h"
#import "TLRechargeHistoryViewController.h"

#import "TLMyBonusViewController.h"

@interface TLWalletViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *navView;
@property (nonatomic, strong) UILabel *navTitleLab;
@property (nonatomic, strong) UIButton *backBtn;

@property (nonatomic, strong) TLWalletHeaderView *walletHeaderView;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation TLWalletViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getUserData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addGradientBackground];
    
    self.walletHeaderView = [TLWalletHeaderView createView];
    self.walletHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 184+PUB_NAVBAR_HEIGHT);
    [self.view addSubview:self.walletHeaderView];
    
    [self.view addSubview:self.tableView];
    MJWeakSelf
    self.walletHeaderView.gotoRechargeCoinsBlock = ^{
        
        TLRechargeCoinsViewController *vc = [TLRechargeCoinsViewController new];
        
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    };
    self.walletHeaderView.gotoBonusListViewBlock = ^{
        
        TLMyBonusViewController *vc = [TLMyBonusViewController new];
        
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    };
    
    [self.view addSubview:self.navView];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TLWalletTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TLWalletTableViewCell"];
    
    if (indexPath.row == 0) {
        cell.titleLab.text = @"Top-up History";
    }else{
        cell.titleLab.text = @"Purchase History";
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        TLRechargeHistoryViewController *vc = [[TLRechargeHistoryViewController alloc] init];
        vc.selectedIndex = 0;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        TLRechargeHistoryViewController *vc = [[TLRechargeHistoryViewController alloc] init];
        vc.selectedIndex = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (UITableView *)tableView{
    if (!_tableView) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,MK_SCREEN_SAVE_BOTTOM_HEIGHT)];
        footView.backgroundColor = UIColor.clearColor;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.walletHeaderView.frame), kScreenWidth, kScreenHeight-CGRectGetMaxY(self.navView.frame)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = footView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = YES;
        if (@available(iOS 15.0, *)) {
            self.tableView.sectionHeaderTopPadding = 0;
        }
        
        [_tableView registerNib:[UINib nibWithNibName:@"TLWalletTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLWalletTableViewCell"];
        
    }
    return _tableView;
}

- (UIView *)navView{
    if (!_navView) {
        _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, PUB_NAVBAR_HEIGHT)];
        [_navView setBackgroundColor:[UIColor clearColor]];
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(6, PUB_NAVBAR_HEIGHT - 28 - 6, 28, 28)];
        [_backBtn setImage:[UIImage imageNamed:@"tzxqy_fh"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_navView addSubview:_backBtn];
        
        _navTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_backBtn.frame)+10, PUB_NAVBAR_HEIGHT - 28 - 6, kScreenWidth-2*(CGRectGetMaxX(_backBtn.frame)+10), 28)];
        _navTitleLab.text = WXLocalizedString(@"我的钱包");
        _navTitleLab.textColor = [UIColor hex_333333];
        _navTitleLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _navTitleLab.textAlignment = NSTextAlignmentCenter;
        [_navView addSubview:_navTitleLab];
        
    }
    return _navView;
}
- (void)getUserData{
    
    MK_WEAK_SELF
    
    UIView *loadinView = [TL_TopAlertManager showLoading:self.view];
    
    [APIManager sendGetRequest:[APIURLManager getUserInfo] parameters:nil model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        [loadinView removeFromSuperview];
        if (success) {
            NSLog(@"%@",requestModel);
            
            TL_UserInfoModel *usermodel = [TL_UserInfoModel mj_objectWithKeyValues:requestModel.data[@"userInfo"]];

            NSDictionary *userDataDict = [usermodel mj_keyValues];
            
            [[NSUserDefaults standardUserDefaults] setObject:userDataDict forKey:@"UserInfoModelDict"];
            
            TL_UserInfoManager.updateWithDict(requestModel.data[@"userInfo"]);
            
            [weakSelf.walletHeaderView loadData];
            
        } else {
            [TL_TopAlertManager showAlertWithType:TL_TopAlertTypeError alertTitle:requestModel.msg ?: WXLocalizedString(@"请求失败")];
            if (requestModel.code == 302) {
                [TL_UserInfoManager logout];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [loadinView removeFromSuperview];
    }];
    
}
@end
